import { bus } from "@/bus";
import { DataSource } from "typeorm";
import { updateGw } from "./persist/gateway";
import { Gateway } from "@/entity/gateway.entity";
export async function setup(AppDataSource: DataSource | null) {
    const gatewayRepo = AppDataSource ? AppDataSource.getRepository(Gateway) : null;
    bus.on('gw.report', async (message: any) => {
        console.log('message', message)
        // 上报升级状态
        if (message.action == 'heartbeat') {
            // 如果 AppDataSource 存在，则存进数据库
            if (AppDataSource && gatewayRepo) {
                const gw = {
                    mac: message.gw_mac,
                    version_bin: message.gw.version_bin,
                    version_fs: message.gw.version_fs,
                    last_report_time: new Date(),
                    time: new Date()
                }
                // 存进数据库 gateway 表
                await updateGw(gw, gatewayRepo);
            } else {
                console.log('AppDataSource is null, skipping database log creation');
            }
        }
    })
}